local daojie = fk.CreateSkill{
  name = "wk_heg__daojie"
}

Fk:loadTranslationTable{
  ["wk_heg__daojie"] = "蹈节",
  [":wk_heg__daojie"] = "与你势力相同的角色于其回合内首次使用非伤害类锦囊牌后，你可以失去1点体力并令其获得之，"..
    "然后你可以令其选择是否变更副将。",

  ["#wk_heg__daojie_change"] = "蹈节：是否令 %dest 变更副将？",
  ["wk_heg__daojie_transform"] = "变更副将",
  ["@@wk_heg__daojie_transform"] = "蹈节 已变更"
}

local H = require "packages.ol_hegemony.util"

daojie:addEffect(fk.CardUseFinished, {
  can_trigger = function(self, event, target, player, data)
    if not (H.compareKingdomWith(player, target) and player:hasSkill(daojie.name) and player.room.current == target) then return false end
    if not data.card.type == Card.TypeTrick or data.card.is_damage_card then return false end
    local room = player.room
    local cardlist = data.card:isVirtual() and data.card.subcards or {data.card.id}
    if #cardlist == 0 or not table.every(cardlist, function (id) return room:getCardArea(id) == Card.Processing end) then return false end
    local logic = room.logic
    local use_event = logic:getCurrentEvent()
    local mark_name = "wk_heg__daojie_record-turn"
    local mark = target:getMark(mark_name)
    if mark == 0 then
      logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local last_use = e.data
        if last_use.from == target and last_use.card.type == Card.TypeTrick and not last_use.card.is_damage_card then
          mark = e.id
          room:setPlayerMark(target, mark_name, mark)
          return true
        end
        return false
      end, Player.HistoryTurn)
    end
    return mark == use_event.id
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:loseHp(player, 1, daojie.name)
    if not target.dead and table.every(Card:getIdList(data.card), function (id) return room:getCardArea(id) == Card.Processing end) then
      room:obtainCard(target, data.card, true, fk.ReasonPrey, player, daojie.name)
    end
    if target == player and player.dead then return end
    if player.dead or target.dead or player:getMark("@@wk_heg__daojie_transform") > 0 then return end
    if room:askToSkillInvoke(player,{
      skill_name = daojie.name,
      prompt = "#wk_heg__daojie_change::"..target.id
    }) then
        local choices = {"wk_heg__daojie_transform", "Cancel"}
        local choice = room:askToChoice(player, {choices = choices, skill_name = daojie.name})
        if choice ~= "Cancel" then
          room:setPlayerMark(player, "@@wk_heg__daojie_transform", 1)
          H.transformGeneral(target, false, 3, false)
        end
    end
  end
})

return daojie